package com.zmx.doctor;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.netease.nim.avchatkit.AVChatKit;
import com.netease.nim.avchatkit.config.AVChatOptions;
import com.netease.nim.avchatkit.model.ITeamDataProvider;
import com.netease.nim.avchatkit.model.IUserInfoProvider;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.auth.LoginInfo;
import com.netease.nimlib.sdk.mixpush.NIMPushClient;
import com.netease.nimlib.sdk.uinfo.model.UserInfo;
import com.netease.nimlib.sdk.util.NIMUtil;
import com.zmx.doctor.view.activity.LoginActivity;
import com.zmx.doctor.view.activity.OrderDetailsActivity;
import com.zmx.doctor.view.activity.WelcomeActivity;
import com.zmx.doctor.view.util.MySharedPreferences;
import com.zmx.doctor.wangyiyun.AVChatInit.AVUserinfoProvider;
import com.zmx.doctor.wangyiyun.mixpush.DemoMixPushMessageHandler;
import com.zmx.doctor.wangyiyun.saveData.DataSaveToLocal;

public class MyApplication extends Application {


    public static String NetworkRequestMode = "post";
    private static MyApplication myApplication;

    public static MyApplication getInstance() {
        return myApplication;
    }


    @Override
    public void onCreate() {
        super.onCreate();

        myApplication = this;
        getInstance();

        NIMClient.init(this,null, NimSDKOptionConfig.getSDKOptions(this));

        if(NIMUtil.isMainProcess(this)){

            initAVChatKit();
            // 注册自定义推送消息处理，这个是可选项
            NIMPushClient.registerMixPushMessageHandler(new DemoMixPushMessageHandler());
        }

    }


    public static String getNetworkRequestMode() {
        return NetworkRequestMode;
    }


    public static boolean isLogin_state() {
        return MySharedPreferences.getInstance(myApplication).getBoolean(MySharedPreferences.isLogin,false);
    }

    public static boolean unread_message() {
        return MySharedPreferences.getInstance(myApplication).getBoolean(MySharedPreferences.unread_message,false);
    }


    /**
     * 初始化AVChatkit
     */
    private void initAVChatKit() {

        AVChatKit.setContext(getApplicationContext());

        AVChatOptions avChatOptions = new AVChatOptions(){
            @Override
            public void logout(Context context) {
//                MainActivity.logout(context, true);
            }
        };
        avChatOptions.entranceActivity = WelcomeActivity.class;
        avChatOptions.notificationIconRes = R.mipmap.ic_stat_notify_msg;
        AVChatKit.init(avChatOptions);

        // 初始化日志系统
//        LogHelper.init();
        // 设置用户相关资料提供者
        AVChatKit.setUserInfoProvider(new IUserInfoProvider() {
            @Override
            public UserInfo getUserInfo(String account) {
                UserInfo userInfo =  AVUserinfoProvider.getInstance().getUserinfo(account);
                if (userInfo == null) {
                    //如果本地获取不到这个人的用户资料
                }
                return userInfo;
            }

            @Override
            public String getUserDisplayName(String account) {
                UserInfo userInfo =  AVUserinfoProvider.getInstance().getUserinfo(account);
                if (userInfo == null) {
                    return "未知用户";
                }else {
                    return userInfo.getName();
                }
            }
        });

        /**
         * 群组信息提供者
         */
        AVChatKit.setTeamDataProvider(new ITeamDataProvider() {
            @Override
            public String getDisplayNameWithoutMe(String teamId, String account) {
                return account; //开发者可以自行用户名展示逻辑
            }

            @Override
            public String getTeamMemberDisplayName(String teamId, String account) {
                return account; //开发者可以自行用户名展示逻辑
            }
        });

    }

//    private DataSaveToLocal dataSaveToLocal = new DataSaveToLocal();
//    //取出本地保存的用户信息
//    private LoginInfo getLoginInfo(){
//
//        String loginData = dataSaveToLocal.readDataToLocal(NimSDKOptionConfig.getAppCacheDir(getApplicationContext())+"logininfo.txt");
//        if(!loginData.isEmpty()){
//            int index1 = loginData.indexOf(",");
//            String accid = loginData.substring(0,index1);
//            String token = loginData.substring(index1+1,loginData.length());
//            LoginInfo loginInfo = new LoginInfo(accid,token);
//            return loginInfo;
//        }else {
//            return null;
//        }
//
//
//    }

}
